home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsIII / newell.c < prev    next >
Text File  |  1992-06-16  |  2KB  |  91 lines

  1. /*
  2. NEWELL'S METHOD FOR COMPUTING THE PLANE EQUATION OF A POLYGON
  3.  
  4.  
  5. Filippo Tampieri
  6. Cornell University
  7. */
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. #include <math.h>
  20.  
  21. /* definition for the components of vectors and plane equations */
  22. #define X   0
  23. #define Y   1
  24. #define Z   2
  25. #define D   3
  26.  
  27. /* a few useful vector operations */
  28. #define VZERO(v)    (v[X] = v[Y] = v[Z] = 0.0)
  29. #define VNORM(v)    (sqrt(v[X] * v[X] + v[Y] * v[Y] + v[Z] * v[Z]))
  30. #define VDOT(u, v)  (u[0] * v[0] + u[1] * v[1] + u[2] * v[2])
  31. #define VINCR(u, v) (u[X] += v[X], u[Y] += v[Y], u[Z] += v[Z])
  32.  
  33. /* type definitions for vectors and plane equations */
  34. typedef float Vector[3];
  35. typedef Vector Point;
  36. typedef Vector Normal;
  37. typedef float Plane[4];
  38.  
  39. /*
  40. **  PlaneEquation--computes the plane equation of an arbitrary
  41. **  3D polygon using Newell's method.
  42. **
  43. **  Entry:
  44. **      verts  - list of the vertices of the polygon
  45. **      nverts - number of vertices of the polygon
  46. **  Exit:
  47. **      plane  - normalized (unit normal) plane equation
  48. */
  49.  
  50. PlaneEquation(verts, nverts, plane)
  51. Point *verts;
  52. int nverts;
  53. Plane plane;
  54. {
  55.     int i;
  56.     Point refpt;
  57.     Normal normal;
  58.     float *u, *v, len;
  59.  
  60.  
  61.  
  62.  
  63.     /* compute the polygon normal and a reference point on
  64.        the plane. Note that the actual reference point is
  65.        refpt / nverts
  66.     */
  67.     VZERO(normal);
  68.     VZERO(refpt);
  69.     for(i = 0; i < nverts; i++) {
  70.         u = verts[i];
  71.         v = verts[(i + 1) % nverts];
  72.         normal[X] += (u[Y] - v[Y]) * (u[Z] + v[Z]);
  73.         normal[Y] += (u[Z] - v[Z]) * (u[X] + v[X]);
  74.         normal[Z] += (u[X] - v[X]) * (u[Y] + v[Y]);
  75.         VINCR(refpt, u);
  76.     }
  77.     /* normalize the polygon normal to obtain the first
  78.        three coefficients of the plane equation
  79.     */
  80.     len = VNORM(normal);
  81.     plane[X] = normal[X] / len;
  82.     plane[Y] = normal[Y] / len;
  83.     plane[Z] = normal[Z] / len;
  84.     /* compute the last coefficient of the plane equation */
  85.     len *= nverts;
  86.     plane[D] = -VDOT(refpt, normal) / len;
  87. }
  88.  
  89.  
  90.  
  91.